package com.quanshu.change.person.view;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.change.broadcast.PayResultBroadcatReciver;
import com.quanshu.change.person.adapter.GoldCharge1Adapter;
import com.quanshu.change.person.adapter.GoldChargeAdapter;
import com.quanshu.change.person.entity.AliPayEntity;
import com.quanshu.change.person.entity.GoldListBean;
import com.quanshu.change.person.entity.WxPayEntity;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.gui.pay.AlipayActivity;
import com.quanshu.exchange.wxapi.WXPayEntryActivity;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoldRechargeActivity extends BaseActivity implements PayResultBroadcatReciver.PayResultSuccessfulListener {


    @BindView(R.id.line_center)
    View lineCenter;
    @BindView(R.id.gold_bean)
    ImageView goldBean;
    @BindView(R.id.gold_bean_txt)
    TextView goldBeanTxt;
    @BindView(R.id.gold_bean_txt1)
    TextView goldBeanTxt1;
    @BindView(R.id.view_title)
    ConstraintLayout viewTitle;
    @BindView(R.id.bag0)
    View bag0;
    @BindView(R.id.txt2)
    TextView txt2;
    @BindView(R.id.rl)
    RecyclerView rl;
    @BindView(R.id.img1)
    ImageView img1;
    @BindView(R.id.txt3)
    TextView txt3;
    @BindView(R.id.recharge_tips)
    ConstraintLayout rechargeTips;
    @BindView(R.id.txt4)
    TextView txt4;
    @BindView(R.id.recharge_pay1_txt)
    TextView rechargePay1Txt;
    @BindView(R.id.recharge_pay1_img)
    ImageView rechargePay1Img;
    @BindView(R.id.recharge_pay1_txt1)
    TextView rechargePay1Txt1;
    @BindView(R.id.recharge_pay1)
    ConstraintLayout rechargePay1;
    @BindView(R.id.recharge_pay2_txt)
    TextView rechargePay2Txt;
    @BindView(R.id.recharge_pay2_img)
    ImageView rechargePay2Img;
    @BindView(R.id.recharge_pay2_txt1)
    TextView rechargePay2Txt1;
    @BindView(R.id.recharge_pay2)
    ConstraintLayout rechargePay2;
    @BindView(R.id.recharge_pay)
    ConstraintLayout rechargePay;
    @BindView(R.id.bag1)
    View bag1;
    @BindView(R.id.txt_ok)
    TextView txtOk;
    @BindView(R.id.rl1)
    RecyclerView rl1;
    @BindView(R.id.title_bar)
    BaseTitleBar titleBar;

    private GoldChargeAdapter goldChargeAdapter;
    private GoldListBean goldListBean;
    private PayResultBroadcatReciver payResultBroadcatReciver;
    private int select = 0;
    private int payState = 0;

    private GoldCharge1Adapter goldCharge1Adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold_recharge);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        unregisterReceiver(payResultBroadcatReciver);
    }

    private void initView() {
        ImmersionBar.with(this).statusBarColor("#FFFFFF").titleBar(titleBar).statusBarDarkFont(true).init();
        payResultBroadcatReciver = new PayResultBroadcatReciver(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PayResultBroadcatReciver.PAY_GOLD_RECHAGR_SUCCESSFUL);
        registerReceiver(payResultBroadcatReciver, intentFilter);
        titleBar.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("购买").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {
                //BaseTool.goActivity(GoldRechargeActivity.this, GoldListActivity.class, null);
            }
        });
        //选择微信支付
        RxViewHelper.getInstance().clicks(rechargePay1, aVoid -> {
            payState = 0;
            setSelelctState(rechargePay1, rechargePay2, rechargePay1Txt, rechargePay2Txt);
        });
        //选择支付宝支付
        RxViewHelper.getInstance().clicks(rechargePay2, aVoid -> {
            payState = 1;
            setSelelctState(rechargePay2, rechargePay1, rechargePay2Txt, rechargePay1Txt);
        });
        //点击充值
        RxViewHelper.getInstance().clicks(txtOk, aVoid -> {
            if (payState == 0) {
                //微信支付
                doWXPay();
            } else {
                //支付宝支付
                doAliPay();
            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        rl.setLayoutManager(gridLayoutManager);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rl1.setLayoutManager(linearLayoutManager);
        getData();
    }

    private void setSelelctState(ConstraintLayout constraintLayout, ConstraintLayout constraintLayout1, TextView textView, TextView textView1) {
        constraintLayout.setEnabled(false);
        constraintLayout1.setEnabled(true);
        textView.setEnabled(false);
        textView1.setEnabled(true);
    }

    //获取金豆充值列表
    private void getData() {
        if (currentUser != null) {
            NetClient.getInstance()
                    .getShopService(ShopInterface.class)
                    .getGoldRechargeList(currentUser.getId())
                    .compose(new BaseTransformer<>(this, true, false))
                    .subscribe(new BaseObserver<BaseResponse<GoldListBean>>() {
                        @Override
                        public void onSuccess(BaseResponse<GoldListBean> response) {
                            if (response.getCode() == 1) {
                                goldListBean = response.getData();
                                goldBeanTxt1.setText(goldListBean.getGold_bean());
                                txt3.setText("活动时间：" + goldListBean.getTime());
                                if (!TextUtils.isEmpty(goldListBean.getTime())) {
                                    rechargeTips.setVisibility(View.VISIBLE);
                                } else {
                                    rechargeTips.setVisibility(View.GONE);
                                }
                                if (goldListBean.getIs_recharge() == 0) {
                                    //关闭
                                    txtOk.setText("活动结束");
                                    txtOk.setEnabled(false);
                                } else if (goldListBean.getIs_recharge() == 1) {
                                    //开启
                                    txtOk.setText("购买");
                                    txtOk.setEnabled(true);
                                }
                                if (goldCharge1Adapter == null && goldListBean.getNumber() != null && goldListBean.getNumber().size() > 0) {
                                    goldListBean.getNumber().get(select).setSelect(true);
                                    if (Integer.parseInt(goldListBean.getNumber().get(select).getNumber())>5000){
                                        //仅支持支付宝支付
                                        rechargePay1.setVisibility(View.GONE);
                                        payState = 1;
                                        setSelelctState(rechargePay2, rechargePay1, rechargePay2Txt, rechargePay1Txt);
                                    }else {
                                        rechargePay1.setVisibility(View.VISIBLE);
                                        //两种都可以支付
                                    }
                                    goldChargeAdapter = new GoldChargeAdapter(response.getData().getNumber());
                                    goldChargeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                        @Override
                                        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                            switch (view.getId()) {
                                                case R.id.item_root:
                                                    goldListBean.getNumber().get(select).setSelect(false);
                                                    select = position;
                                                    goldListBean.getNumber().get(select).setSelect(true);
                                                    if (Integer.parseInt(goldListBean.getNumber().get(select).getNumber())>5000){
                                                        //仅支持支付宝支付
                                                        rechargePay1.setVisibility(View.GONE);
                                                        payState = 1;
                                                        setSelelctState(rechargePay2, rechargePay1, rechargePay2Txt, rechargePay1Txt);
                                                    }else {
                                                        rechargePay1.setVisibility(View.VISIBLE);
                                                        //两种都可以支付
                                                    }
                                                    break;
                                            }
                                            goldChargeAdapter.notifyDataSetChanged();
                                        }
                                    });
                                    goldCharge1Adapter = new GoldCharge1Adapter(goldListBean.getNumber());
                                    rl.setAdapter(goldChargeAdapter);
                                    rl1.setAdapter(goldCharge1Adapter);
                                } else {
                                    goldCharge1Adapter.setNewData(response.getData().getNumber());
                                }
                            }
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LogUtil.printException(e);
                        }
                    });
        }
    }


    private void doWXPay() {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .getGoldWxPay(currentUser.getId() + "", goldListBean.getNumber().get(select).getId())
                .compose(new BaseTransformer<>(this, true, false))
                .subscribe(new BaseObserver<BaseResponse<WxPayEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<WxPayEntity> response) {
                        if (response.getCode() == 1) {
                            Intent intent = new Intent(GoldRechargeActivity.this, WXPayEntryActivity.class);
                            intent.putExtra("url", response.getData().getWxPayString());
                            intent.putExtra("state", 1);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });
    }

    private void doAliPay() {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .getGoldAliPay(currentUser.getId() + "", goldListBean.getNumber().get(select).getId())
                .compose(new BaseTransformer<>(this, true, false))
                .subscribe(new BaseObserver<BaseResponse<AliPayEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<AliPayEntity> response) {
                        if (response.getCode() == 1) {
                            Intent intent = new Intent(GoldRechargeActivity.this, AlipayActivity.class);
                            intent.putExtra("url", "http://www.cattleshop.cn/cattle_exchange_show/goApp?aliPayString=" + response.getData().getAliPayString());
                            intent.putExtra("state", 1);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });
    }

    @Override
    public void paySuccess() {
        getData();
    }
}
